/* 
    JSPWiki - a JSP-based WikiWiki clone.

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.  
 */
package com.ecyrd.jspwiki.providers;

import java.io.IOException;
import java.util.*;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.ecyrd.jspwiki.*;
import com.ecyrd.jspwiki.parser.MarkupParser;
import com.ecyrd.jspwiki.render.RenderingManager;
import com.ecyrd.jspwiki.util.ClassUtil;
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheFactory;
import net.sf.jsr107cache.CacheManager;

/**
 *  Provides a caching page provider.  This class rests on top of a
 *  real provider class and provides a cache to speed things up.  Only
 *  if the cache copy of the page text has expired, we fetch it from
 *  the provider.
 *  <p>
 *  This class also detects if someone has modified the page
 *  externally, not through JSPWiki routines, and throws the proper
 *  RepositoryModifiedException.
 *  <p>
 *  Heavily based on ideas by Chris Brooking.
 *  <p>
 *  Since 2.1.52 uses the OSCache library from OpenSymphony.
 *
 *  @since 1.6.4
 *  @see RepositoryModifiedException
 */
// FIXME: Synchronization is a bit inconsistent in places.
// FIXME: A part of the stuff is now redundant, since we could easily use the text cache
//        for a lot of things.  RefactorMe.

public class CachingProvider
    implements WikiPageProvider, VersioningProvider
{
    private static final Logger log = Logger.getLogger(CachingProvider.class.getName());

    private WikiPageProvider m_provider;
    // FIXME: Find another way to the search engine to use instead of from WikiEngine?
    private WikiEngine       m_engine;
    
    private Cache cache;
    
    private final String WIKI_PAGE_NAME_WIKI_PAGE_MAP = "WIKI_PAGE_NAME_WIKI_PAGE";
    private final String NON_EXISTENT_WIKI_PAGES_MAP = "NON_EXISTENT_WIKI_PAGES";
    private final String WIKI_PAGE_WIKI_TEXT_MAP = "WIKI_PAGE_WIKI_TEXT";
    private final String WIKI_PAGE_HISTORY_MAP = "WIKI_PAGE_HISTORY";

    private Map<String, WikiPage>            m_cache;
    private Map<String, String>            m_negCache; // Cache for holding non-existing pages

    private Map<String, String>            m_textCache;
    private Map<String, List>            m_historyCache;

    private long             m_cacheMisses = 0;
    private long             m_cacheHits   = 0;

    private long             m_historyCacheMisses = 0;
    private long             m_historyCacheHits   = 0;

    private int              m_expiryPeriod = 30;

    /**
     *  This can be very long, as normally all modifications are noticed in an earlier
     *  stage.
     */
    private int              m_pageContentExpiryPeriod = 24*60*60;

    // FIXME: This MUST be cached somehow.

    private boolean          m_gotall = false;

    /**
     *  Defines, in seconds, the amount of time a text will live in the cache
     *  at most before requiring a refresh.
     */

    public static final String PROP_CACHECHECKINTERVAL = "jspwiki.cachingProvider.cacheCheckInterval";
    
    /**
     *  The capacity of the cache.
     */
    public static final String PROP_CACHECAPACITY      = "jspwiki.cachingProvider.capacity";

    private static final int   DEFAULT_CACHECAPACITY   = 1000; // Good most wikis

    private static final String OSCACHE_ALGORITHM      = "com.opensymphony.oscache.base.algorithm.LRUCache";

    /**
     *  {@inheritDoc}
     */
    public void initialize( WikiEngine engine, Properties properties )
        throws NoRequiredPropertyException,
               IOException
    {
        log.log(Level.INFO,"Initing CachingProvider");

        // engine is used for getting the search engine
        m_engine = engine;

        //
        //  Cache consistency checks
        //
        m_expiryPeriod = TextUtil.getIntegerProperty( properties,
                                                      PROP_CACHECHECKINTERVAL,
                                                      m_expiryPeriod );

        log.log(Level.INFO,"Cache expiry period is "+m_expiryPeriod+" s");

        //
        //  Text cache capacity
        //
        int capacity = TextUtil.getIntegerProperty( properties,
                                                    PROP_CACHECAPACITY,
                                                    DEFAULT_CACHECAPACITY );

        log.log(Level.INFO,"Cache capacity "+capacity+" pages.");
        
        CacheFactory cacheFactory = null;
		try {
			cacheFactory = CacheManager.getInstance().getCacheFactory();
			Map props = new HashMap();
	        props.put(GCacheFactory.EXPIRATION_DELTA, m_expiryPeriod);
	        cache = cacheFactory.createCache(props);
		} catch (CacheException e) {
			log.log(Level.SEVERE, "Exception in getting the Cache", e);
		}

        m_cache = (Map<String, WikiPage>) cache.get(WIKI_PAGE_NAME_WIKI_PAGE_MAP);
        if (m_cache == null) {
        	m_cache = new HashMap<String, WikiPage>();
        } else {
        	Collection<WikiPage> wikiPages = m_cache.values();
        	//Set the Wiki Engine for all the pages.
        	for (WikiPage wikiPage:wikiPages) {
        		if (wikiPage != null)
        			wikiPage.setEngine(engine);
        	}
        }

        //
        //  OSCache documentation sucks big time.  The clazz-parameter is completely
        //  undefined; I had to read the source code to figure out that you need
        //  to declare what type of a listener you are adding by sending the type
        //  of the interface.
        //
        //m_cache.addCacheEventListener( m_allCollector, CacheEntryEventListener.class );

        //
        //  FIXME: There's an interesting issue here... It would probably be
        //  possible to DOS a JSPWiki instance by bombarding it with names that
        //  do not exist, as they would fill the negcache.  Will need to
        //  think about this some more...
        //
        m_negCache = (Map<String, String>) cache.get(NON_EXISTENT_WIKI_PAGES_MAP);
        if (m_negCache == null) {
        	m_negCache = new HashMap<String, String>();
        }

        m_textCache = (Map<String, String>) cache.get(WIKI_PAGE_WIKI_TEXT_MAP);
        if (m_textCache == null) {
        	m_textCache = new HashMap<String, String>();
        }

        m_historyCache = (Map<String, List>) cache.get(WIKI_PAGE_HISTORY_MAP);
        if (m_historyCache == null) {
        	m_historyCache = new HashMap<String, List>();
        }

        //
        //  Find and initialize real provider.
        //
        String classname = WikiEngine.getRequiredProperty( properties,
                                                           PageManager.PROP_PAGEPROVIDER );


        try
        {
            Class providerclass = ClassUtil.findClass( "com.ecyrd.jspwiki.providers",
                                                       classname );

            m_provider = (WikiPageProvider)providerclass.newInstance();

            log.log(Level.INFO,"Initializing real provider class "+m_provider);
            m_provider.initialize( engine, properties );
        }
        catch( ClassNotFoundException e )
        {
            log.log(Level.SEVERE,"Unable to locate provider class "+classname,e);
            throw new IllegalArgumentException("no provider class");
        }
        catch( InstantiationException e )
        {
            log.log(Level.SEVERE,"Unable to create provider class "+classname,e);
            throw new IllegalArgumentException("faulty provider class");
        }
        catch( IllegalAccessException e )
        {
            log.log(Level.SEVERE,"Illegal access to provider class "+classname,e);
            throw new IllegalArgumentException("illegal provider class");
        }
    }




    private WikiPage getPageInfoFromCache( String name )
        throws ProviderException,
               RepositoryModifiedException
    {
        boolean wasUpdated = false;
        // Sanity check; seems to occur sometimes
        if( name == null ) return null;

        WikiPage item = (WikiPage)m_cache.get( name );

        wasUpdated = true;
        WikiPage refreshed;
        if( item != null ) {
            return item;
        } else {
            //
            //  Just be careful that we don't accidentally leave the cache in a
            //  hung state
            //

            refreshed = m_provider.getPageInfo( name, WikiPageProvider.LATEST_VERSION );
            try {
	            if( refreshed == null )
	            {
	                log.log(Level.INFO,"Page "+name+" has been removed externally.");
	
	                m_cache.put( name, null );
	                m_textCache.put( name, null );
	                m_historyCache.put( name, null );
	                // We cache a page miss
	                m_negCache.put( name, name );
	                wasUpdated = true;
	
	                //throw new RepositoryModifiedException( "Removed: "+name, name );
	                return null;
	            }
	            else
	            {
	                // We must now add it
	                m_cache.put( name, refreshed );
	                // Requests for this page are now no longer denied
	                m_negCache.put( name, null );
	                wasUpdated = true;
	
	                return refreshed;
	                //throw new RepositoryModifiedException( "Added: "+name, name );
	            }
            } finally {
            	cache.put(NON_EXISTENT_WIKI_PAGES_MAP, m_negCache);
            	cache.put(WIKI_PAGE_HISTORY_MAP, m_historyCache);
            	cache.put(WIKI_PAGE_NAME_WIKI_PAGE_MAP, m_cache);
            	cache.put(WIKI_PAGE_WIKI_TEXT_MAP, m_textCache);
            }
        }
    }

    /**
     *  {@inheritDoc}
     */
    public boolean pageExists( String pageName, int version )
    {
        if( pageName == null ) return false;

        //
        //  First, check the negative cache if we've seen it before
        //
        String isNonExistant = (String) m_negCache.get( pageName );

        if( isNonExistant != null ) return false; // No such page

        WikiPage p = null;

        try
        {
            p = getPageInfoFromCache( pageName );
        }
        catch( RepositoryModifiedException e )
        {
            // The repository was modified, we need to check now if the page was removed or
            // added.
            // TODO: This information would be available in the exception, but we would
            //       need to subclass.

            try
            {
                p = getPageInfoFromCache( pageName );
            }
            catch( Exception ex )
            {
                // This should not happen
                return false;
            }
        }
        catch( ProviderException e )
        {
            log.log(Level.INFO,"Provider failed while trying to check if page exists: "+pageName);
            return false;
        }

        if( p != null )
        {
            int latestVersion = p.getVersion();

            if( version == latestVersion || version == LATEST_VERSION )
            {
                return true;
            }

            if( m_provider instanceof VersioningProvider )
                return ((VersioningProvider) m_provider).pageExists( pageName, version );
        }

        try
        {
            return getPageInfo( pageName, version ) != null;
        }
        catch( ProviderException e )
        {}

        return false;
    }

    /**
     *  {@inheritDoc}
     */
    public boolean pageExists( String pageName )
    {
        if( pageName == null ) return false;

        //
        //  First, check the negative cache if we've seen it before
        //
        String isNonExistant = (String) m_negCache.get( pageName );

        if( isNonExistant != null ) return false; // No such page

        WikiPage p = null;

        try
        {
            p = getPageInfoFromCache( pageName );
        }
        catch( RepositoryModifiedException e )
        {
            // The repository was modified, we need to check now if the page was removed or
            // added.
            // TODO: This information would be available in the exception, but we would
            //       need to subclass.

            try
            {
                p = getPageInfoFromCache( pageName );
            }
            catch( Exception ex )
            {
                return false; // This should not happen
            }
        }
        catch( ProviderException e )
        {
            log.log(Level.INFO,"Provider failed while trying to check if page exists: "+pageName);
            return false;
        }

        //
        //  A null item means that the page either does not
        //  exist, or has not yet been cached; a non-null
        //  means that the page does exist.
        //
        if( p != null )
        {
            return true;
        }

        //
        //  If we have a list of all pages in memory, then any page
        //  not in the cache must be non-existent.
        //
        //  FIXME: There's a problem here; if someone modifies the
        //         repository by adding a page outside JSPWiki,
        //         we won't notice it.

        if( m_gotall )
        {
            return false;
        }

        //
        //  We could add the page to the cache here as well,
        //  but in order to understand whether that is a
        //  good thing or not we would need to analyze
        //  the JSPWiki calling patterns extensively.  Presumably
        //  it would be a good thing if pageExists() is called
        //  many times before the first getPageText() is called,
        //  and the whole page is cached.
        //
        return m_provider.pageExists( pageName );
    }

    /**
     *  {@inheritDoc}
     *  @throws RepositoryModifiedException If the page has been externally modified.
     */
    public String getPageText( String pageName, int version )
        throws ProviderException,
               RepositoryModifiedException
    {
        String result = null;

        if( pageName == null ) return null;

        if( version == WikiPageProvider.LATEST_VERSION )
        {
            result = getTextFromCache( pageName );
        }
        else
        {
            WikiPage p = getPageInfoFromCache( pageName );

            //
            //  Or is this the latest version fetched by version number?
            //
            if( p != null && p.getVersion() == version )
            {
                result = getTextFromCache( pageName );
            }
            else
            {
                result = m_provider.getPageText( pageName, version );
            }
        }

        return result;
    }


    /**
     *  @throws RepositoryModifiedException If the page has been externally modified.
     */
    private String getTextFromCache( String pageName )
        throws ProviderException,
               RepositoryModifiedException
    {
        String text;
        boolean wasUpdated = false;

        if( pageName == null ) return null;

        WikiPage page = getPageInfoFromCache( pageName );

        text = (String)m_textCache.get( pageName);
        wasUpdated = true;

        if( text == null )
        {
            if( page != null )
            {
                text = m_provider.getPageText( pageName, WikiPageProvider.LATEST_VERSION );

                m_textCache.put( pageName, text );
                cache.put(WIKI_PAGE_WIKI_TEXT_MAP, m_textCache);
                m_cacheMisses++;
            }
            else
            {
                return null;
            }
        }
        else
        {
            m_cacheHits++;
        }

        return text;
    }

    /**
     *  {@inheritDoc}
     */
    public void putPageText( WikiPage page, String text )
        throws ProviderException
    {
        synchronized(this)
        {
            m_provider.putPageText( page, text );

            page.setLastModified( new Date() );

            // Refresh caches properly

            m_cache.remove( page.getName() );
            m_textCache.put( page.getName(), text );
            m_historyCache.remove( page.getName() );
            m_negCache.remove( page.getName() );

            // Refresh caches
            try
            {
                getPageInfoFromCache( page.getName() );
            }
            catch(RepositoryModifiedException e) {} // Expected
        }
    }

    /**
     *  {@inheritDoc}
     */
    public Collection getAllPages()
        throws ProviderException
    {
        Collection all;

        if( m_gotall == false )
        {
            all = m_provider.getAllPages();

            // Make sure that all pages are in the cache.

            synchronized(this)
            {
                for( Iterator i = all.iterator(); i.hasNext(); )
                {
                    WikiPage p = (WikiPage) i.next();

                    m_cache.put( p.getName(), p );
                    // Requests for this page are now no longer denied
                    m_negCache.put( p.getName(), null );
                }
                cache.put(NON_EXISTENT_WIKI_PAGES_MAP, m_negCache);
            	cache.put(WIKI_PAGE_NAME_WIKI_PAGE_MAP, m_cache);
                m_gotall = true;
            }
        }
        else
        {
            all = m_cache.values();
            all.remove(null);
        }

        return all;
    }

    /**
     *  {@inheritDoc}
     */
    public Collection getAllChangedSince( Date date )
    {
        return m_provider.getAllChangedSince( date );
    }

    /**
     *  {@inheritDoc}
     */
    public int getPageCount()
        throws ProviderException
    {
        return m_provider.getPageCount();
    }

    /**
     *  {@inheritDoc}
     */
    public Collection findPages( QueryItem[] query )
    {
        //
        //  If the provider is a fast searcher, then
        //  just pass this request through.
        //
        return m_provider.findPages( query );

        // FIXME: Does not implement fast searching
    }

    //
    //  FIXME: Kludge: make sure that the page is also parsed and it gets all the
    //         necessary variables.
    //

    private void refreshMetadata( WikiPage page )
    {
        if( page != null && !page.hasMetadata() )
        {
            RenderingManager mgr = m_engine.getRenderingManager();

            try
            {
                String data = m_provider.getPageText(page.getName(), page.getVersion());

                WikiContext ctx = new WikiContext( m_engine, page );
                MarkupParser parser = mgr.getParser( ctx, data );

                parser.parse();
            }
            catch( Exception ex )
            {
                log.log(Level.INFO,"Failed to retrieve variables for wikipage "+page);
            }
        }
    }

    /**
     *  {@inheritDoc}
     */
    public WikiPage getPageInfo( String pageName, int version )
        throws ProviderException
    {
        WikiPage page = null;
        WikiPage cached = getPageInfoFromCache( pageName );

        int latestcached = (cached != null) ? cached.getVersion() : Integer.MIN_VALUE;

        if( version == WikiPageProvider.LATEST_VERSION ||
            version == latestcached )
        {
            if( cached == null )
            {
                WikiPage data = m_provider.getPageInfo( pageName, version );

                if( data != null )
                {
                    m_cache.put( pageName, data );
                    // Requests for this page are now no longer denied
                    m_negCache.put( pageName, null );
                    cache.put(NON_EXISTENT_WIKI_PAGES_MAP, m_negCache);
                	cache.put(WIKI_PAGE_NAME_WIKI_PAGE_MAP, m_cache);
                }
                page = data;
            }
            else
            {
                page = cached;
            }
        }
        else
        {
            // We do not cache old versions.
            page = m_provider.getPageInfo( pageName, version );
            //refreshMetadata( page );
        }

        refreshMetadata( page );

        return page;
    }

    /**
     *  {@inheritDoc}
     */
    public List getVersionHistory( String pageName )
        throws ProviderException
    {
        List history = null;
        boolean wasUpdated = false;

        if( pageName == null ) return null;
        history = (List)m_historyCache.get( pageName );

        log.log(Level.INFO,"History cache hit for page "+pageName);
        m_historyCacheHits++;
        wasUpdated = true;
        if (history == null)
        {
            history = m_provider.getVersionHistory( pageName );

            m_historyCache.put( pageName, history );

            log.log(Level.INFO,"History cache miss for page "+pageName);
            m_historyCacheMisses++;
            wasUpdated = true;
            cache.put(WIKI_PAGE_HISTORY_MAP, m_historyCache);
        }

        return history;
    }

    /**
     *  {@inheritDoc}
     */
    public synchronized String getProviderInfo()
    {
        return "Real provider: "+m_provider.getClass().getName()+
               ". Cache misses: "+m_cacheMisses+
               ". Cache hits: "+m_cacheHits+
               ". History cache hits: "+m_historyCacheHits+
               ". History cache misses: "+m_historyCacheMisses+
               ". Cache consistency checks: "+m_expiryPeriod+"s";
    }

    /**
     *  {@inheritDoc}
     */
    public void deleteVersion( String pageName, int version )
        throws ProviderException
    {
        //
        //  Luckily, this is such a rare operation it is okay
        //  to synchronize against the whole thing.
        //
        synchronized( this )
        {
            WikiPage cached = getPageInfoFromCache( pageName );

            int latestcached = (cached != null) ? cached.getVersion() : Integer.MIN_VALUE;

            //
            //  If we have this version cached, remove from cache.
            //
            if( version == WikiPageProvider.LATEST_VERSION ||
                version == latestcached )
            {
                m_cache.remove( pageName );
                m_textCache.remove( pageName );
                m_historyCache.remove( pageName );
            }
            m_provider.deleteVersion( pageName, version );
            cache.put(WIKI_PAGE_HISTORY_MAP, m_historyCache);
            cache.put(WIKI_PAGE_WIKI_TEXT_MAP, m_textCache);
            cache.put(WIKI_PAGE_NAME_WIKI_PAGE_MAP, m_cache);
        }
    }

    /**
     *  {@inheritDoc}
     */
    public void deletePage( String pageName )
        throws ProviderException
    {
        //
        //  See note in deleteVersion().
        //
        synchronized(this)
        {
            m_cache.put( pageName, null );
            m_textCache.put( pageName, null );
            m_historyCache.put( pageName, null );
            m_negCache.put( pageName, pageName );
            m_provider.deletePage( pageName );
            cache.put(WIKI_PAGE_HISTORY_MAP, m_historyCache);
            cache.put(WIKI_PAGE_WIKI_TEXT_MAP, m_textCache);
            cache.put(WIKI_PAGE_NAME_WIKI_PAGE_MAP, m_cache);
            cache.put(NON_EXISTENT_WIKI_PAGES_MAP, m_negCache);
        }
    }

    /**
     *  {@inheritDoc}
     */
    public void movePage( String from,
                          String to )
        throws ProviderException
    {
        m_provider.movePage( from, to );

        synchronized(this)
        {
            // Clear any cached version of the old page
            log.log(Level.INFO,"Removing from page "+from+" from cache");
//            m_cache.removeEntry( from );
            m_cache.put( from, null );
            m_textCache.put( from, null );
            m_historyCache.put( from, null );
            m_negCache.put( from, from );
            // Clear the cache for the to page, if that page already exists
            //if ( m_cache.get( to ) != null )
            //{
                log.log(Level.INFO,"Removing to page "+to+" from cache");
//                m_cache.removeEntry( to );
                m_cache.put( to, null );
                m_textCache.put( to, null );
                m_historyCache.put( to, null );
                m_negCache.put( to, to );
                cache.put(WIKI_PAGE_HISTORY_MAP, m_historyCache);
                cache.put(WIKI_PAGE_WIKI_TEXT_MAP, m_textCache);
                cache.put(WIKI_PAGE_NAME_WIKI_PAGE_MAP, m_cache);
                cache.put(NON_EXISTENT_WIKI_PAGES_MAP, m_negCache);
            //}
        }
    }

    /**
     *  Returns the actual used provider.
     *  @since 2.0
     *  @return The real provider.
     */
    public WikiPageProvider getRealProvider()
    {
        return m_provider;
    }
}
